<%@page import="edu.mta.pokerstats.Game"%>
<%@page import="edu.mta.pokerstats.GameRecord"%>
<%@page import="java.util.ArrayList"%>
<%@page import="edu.mta.pokerstats.Player"%>
<%@page import="edu.mta.pokerstats.Team"%>
<%@page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%
	Player viewingPlayer = (Player)session.getAttribute("viewingPlayer");
	if (viewingPlayer.isTeamLeader()) {
%>
<script type="text/javascript">
	$(document).ready(function () {
		$('#ApprovedTable').dataTable({
			"iDisplayLength": 50,
		    "aLengthMenu": [[10, 25, 50, -1], [10, 25, 50, "All"]],
			"aoColumnDefs": [
					{ 'bSortable': false, 'aTargets': [3] },
					{ 'bSearchable': false, 'aTargets': [3]}
			]
		});
		
		$('#PendingTable').dataTable({
			"iDisplayLength": 50,
		    "aLengthMenu": [[10, 25, 50, -1], [10, 25, 50, "All"]],
			"aoColumnDefs": [
					{ 'bSortable': false, 'aTargets': [3] },
					{ 'bSearchable': false, 'aTargets': [3]}
			]
		});
		
		$('#DeclinedTable').dataTable({
			"iDisplayLength": 50,
		    "aLengthMenu": [[10, 25, 50, -1], [10, 25, 50, "All"]],
			"aoColumnDefs": [
					{ 'bSortable': false, 'aTargets': [3] },
					{ 'bSearchable': false, 'aTargets': [3]}
			]
		});
	
	});

	function acceptPlayer(i_PlayerID, i_TableName, i_RowNum){
		$.ajax({
			url : "servletPlayer",
			type : "POST",
			data : {
				action : "changeJoinStatus",
				playerID : i_PlayerID,
				joinStatus : "Approved"
			},
			success : function(data, status) {
				refreshTables();
			}
		});
	}
	
	function declinePlayer(i_PlayerID){
		$.ajax({
			url : "servletPlayer",
			type : "POST",
			data : {
				action : "changeJoinStatus",
				playerID : i_PlayerID,
				joinStatus : "Declined"
			},
			success : function(data, status) {
				refreshTables();
			}
		});
	}
	
	function refreshTables(){
		$.ajax({
			url : "ShowJoinRequests.jsp",
			type : "POST",
			success : function(data, status) {
				changeDivMainContainer(data);
			}
		});
	}
	</script>
<%
	Team playerTeam = Team.getTeamById(viewingPlayer.getTeamId());
	ArrayList<Player> playersList = playerTeam.getPlayersInTeam();
%>
<div>
<h3>Players already in Team "<%=playerTeam.getName()%>"</h3>
<table id="ApprovedTable" class="display">
	<thead>
		<tr>
			<th>Name</th>
			<th>Username</th>
			<th>Birthday</th>
			<th>Actions</th>
		</tr>
	</thead>
	<tbody>
	<%
		for (int i = 0; i < playersList.size(); i++) {
			Player currPlayer = playersList.get(i);
	%>
		<tr id="ApprovedRow<%=i + 1%>">
			<td><%=currPlayer.getName()%></td>
			<td><%=currPlayer.getUsername()%></td>
			<td><%= currPlayer.getBirthday() != null? currPlayer.getBirthday() : "Unknown"%></td>
			<td>
				<button <%=currPlayer.isTeamLeader()? "title=\"Cannot kick Team Leader\" disabled=\"disabled\" " : ""%> onclick="declinePlayer(<%=currPlayer.getId()%>)">Kick Player</button>
			</td>
		</tr>
	<%
		}
	%>
	</tbody>
</table>
</div>

<br>

<div>
<h3>Players who want to be in Team "<%=playerTeam.getName()%>"</h3>
<% playersList = playerTeam.getPlayersInTeam("Pending"); %>
<table id="PendingTable" class="display">
	<thead>
		<tr>
			<th>Name</th>
			<th>Username</th>
			<th>Birthday</th>
			<th>Actions</th>
		</tr>
	</thead>
	<tbody>
	<%
		for (int i = 0; i < playersList.size(); i++) {
			Player currPlayer = playersList.get(i);
	%>
		<tr id="PendingRow<%=i + 1%>">
			<td><%=currPlayer.getName()%></td>
			<td><%=currPlayer.getUsername()%></td>
			<td><%= currPlayer.getBirthday() != null? currPlayer.getBirthday() : "Unknown"%></td>
			<td>
				<button onclick="acceptPlayer(<%=currPlayer.getId()%>,'Pending',<%=i + 1%>)">Accept</button>
				<button onclick="declinePlayer(<%=currPlayer.getId()%>)">Decline</button>
			</td>
		</tr>
	<%
		}
	%>
	</tbody>
</table>
</div>

<br>

<div>
<h3>Players that have not been approved for Team "<%=playerTeam.getName()%>"</h3>
<% playersList = playerTeam.getPlayersInTeam("Declined"); %>
<table id="DeclinedTable" class="display">
	<thead>
		<tr>
			<th>Name</th>
			<th>Username</th>
			<th>Birthday</th>
			<th>Actions</th>
		</tr>
	</thead>
	<tbody>
	<%
		for (int i = 0; i < playersList.size(); i++) {
			Player currPlayer = playersList.get(i);
	%>
		<tr id="DeclinedRow<%=i + 1%>">
			<td><%=currPlayer.getName()%></td>
			<td><%=currPlayer.getUsername()%></td>
			<td><%= currPlayer.getBirthday() != null? currPlayer.getBirthday() : "Unknown"%></td>
			<td>
				<button onclick="acceptPlayer(<%=currPlayer.getId()%>,'Declined',<%=i + 1%>)">Accept</button>
			</td>
		</tr>
	<%
		}
	%>
	</tbody>
</table>
</div>
<%}
	else {
%>
	<h3>You are not allowed to view this page!</h3>
<%		
	}
%>